package by.siarhei.java.core.io;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author Siarhei Shchahratsou <s.siarhei@gmail.com>
 * @since 12.11.13
 *
 * 0x5A - 'Z' 0x6C - 'l' 0x61 - 'a'
 */
public class FileInputStreamExample {
    public static final String FILE_PATH = System.getProperty("user.dir") + "\\src\\main\\resources\\footballers.txt";
    public static final Logger LOGGER = LoggerFactory.getLogger(FileInputStreamExample.class);

    public static void main(String[] args) throws Exception {
        LOGGER.info("file.path={}", FILE_PATH);

        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(FILE_PATH);
            int bytesAvailable = fileInputStream.available();
            LOGGER.info("Bytes available: {}", bytesAvailable);
            if (bytesAvailable > 0) {
                byte[] data = new byte[bytesAvailable];
                int readBytes = fileInputStream.read(data);
                LOGGER.info("Read bytes: {}\nbytes: {}", readBytes, Arrays.toString(data));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
